﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PMS.Infrastructure;
using Microsoft.EntityFrameworkCore;
using System.Reflection;
using MediatR;
using PMS.Domain.AggregatesModel.MessaeAggregate;
using PMS.Infrastructure.Repositories;
using PMS.Domain.AggregatesModel.ProjectAggregate;
using PMS.Application.ProjectApp;

namespace PMS.Web
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //添加DbContext依赖注入          
            services.AddDbContext<PMSContext>(option =>
            {
                option.UseSqlServer(Configuration.GetConnectionString("ProjectConnect"), sql =>
                {
                    sql.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
                });
            });

            //添加MediatR注入
            services.AddMediatR(new []{ typeof(Startup).GetTypeInfo().Assembly, Assembly.Load("PMS.Application"),Assembly.Load("PMS.Domain"), Assembly.Load("PMS.Infrastructure") });

            #region 服务注册

            services.AddScoped<IMessageCenterRepository, MessageCenterRepository>()
                     .AddScoped<IProjectRepository, ProjectRepository>()
                     .AddScoped<IProjectService, ProjectService>();

            #endregion

            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
